package org.springblade.pm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.pm.entity.Plan;
import org.springblade.pm.entity.PlanDetails;
import org.springblade.pm.entity.PlanMilestone;
import org.springblade.pm.mapper.PlanDetailsMapper;
import org.springblade.pm.mapper.PlanMapper;
import org.springblade.pm.mapper.PlanMilestoneMapper;
import org.springblade.pm.service.IPlanService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class PlanServiceImpl extends ServiceImpl<PlanMapper,Plan> implements IPlanService{
	PlanMapper planMapper;
	PlanDetailsMapper planDetailsMapper;
	PlanMilestoneMapper planMilestoneMapper;

	@Override
	public Plan selectPlan(Integer projectId, String version) {
		//根据version和项目id查询到项目计划主表
		QueryWrapper<Plan> wrapper1 = new QueryWrapper<>();
		wrapper1.eq("pid",projectId).eq("version",version);
		Plan plan = planMapper.selectOne(wrapper1);
		//根据主表id查询到项目进度
		QueryWrapper<PlanDetails> wrapper2 = new QueryWrapper<>();
		wrapper2.eq("pid",plan.getId()).orderByAsc("sort");
		List<PlanDetails> pdList = planDetailsMapper.selectList(wrapper2);
		//遍历项目进度信息查询计划里程碑信息
		for (PlanDetails planDetails:pdList){
			QueryWrapper<PlanMilestone> wrapper3 = new QueryWrapper<>();
			wrapper3.eq("pid",planDetails.getId()).orderByAsc("sort");
			List<PlanMilestone> pmList = planMilestoneMapper.selectList(wrapper3);
			planDetails.setChildren(pmList);
		}
		plan.setPdList(pdList);
		return plan;
	}

	@Override
	public PlanDetails selectPlanDetails(Integer pid, Integer sort) {
		QueryWrapper<PlanDetails> wrapper = new QueryWrapper<>();
		wrapper.eq("pid",pid).eq("sort",sort);
		return	planDetailsMapper.selectOne(wrapper);
	}

	@Override
	public Integer savePlanDetails(PlanDetails planDetails) {
		Integer id = planDetails.getId();
		if (id==null||"".equals(id.toString())){
			return planDetailsMapper.insert(planDetails);
		}else{
			return planDetailsMapper.updateById(planDetails);
		}

	}


	@Override
	public Integer savePlanMilestone(PlanMilestone planMilestone) {
		Integer id = planMilestone.getId();
		if(id==null||"".equals(id.toString())){
			return planMilestoneMapper.insert(planMilestone);
		}else{
			return planMilestoneMapper.updateById(planMilestone);
		}
	}

	@Override
	public R getplanDetail(Integer id, String sort) {
		if (sort.contains(".")){
			return R.data(planMilestoneMapper.selectById(id));
		}else{
			return R.data(planDetailsMapper.selectById(id));
		}
	}

	@Override
	public void deletePlan(Integer id) {
		planMapper.deleteById(id);
		QueryWrapper<PlanDetails> wrapper = new QueryWrapper<>();
		wrapper.eq("pid",id);
		List<PlanDetails> list = planDetailsMapper.selectList(wrapper);
		for (PlanDetails pd:list){
			planDetailsMapper.deleteById(pd.getId());
			QueryWrapper<PlanMilestone> wrapper1 = new QueryWrapper<>();
			wrapper.eq("pid",id);
			List<PlanMilestone> pmList = planMilestoneMapper.selectList(wrapper1);
			for(PlanMilestone pm:pmList){
				planMilestoneMapper.deleteById(pm.getId());
			}
		}


	}
}
